
/**
 *
 */
package com.serviceprovider.business.module.admin.transaction.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.transaction.model.TransactionTarget;
import com.serviceprovider.business.module.admin.transaction.services.dao.interfaces.ITransactionDataService;
import com.serviceprovider.business.module.admin.user.services.dao.interfaces.IUserService;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * @author mrduongnv@gmail.com
 *
 */
@Controller
public class TransactionDataController {
    @Autowired
    private ITransactionDataService transactionDataService;
    @Autowired
    IUserService                    userService;

    @ModelAttribute
    @RequestMapping(
        value  = RequestConstants.TransactionRequests.INSERT_TARGET_DATA,
        method = RequestMethod.GET
    )
    public ModelAndView showForm() {
        ModelAndView                  modelAndView             = new ModelAndView();
        final List<TransactionTarget> retrievedTransformedData = this.transactionDataService.retrieveTransformedData();

        modelAndView.addObject("retrievedTransformedData", retrievedTransformedData);

        return modelAndView;
    }

    @RequestMapping(
        value  = RequestConstants.TransactionRequests.INSERT_TARGET_DATA,
        method = RequestMethod.POST
    )
    public ModelAndView onSubmit(HttpServletRequest request) throws Exception {
        ModelAndView mov = new ModelAndView(RequestConstants.TransactionRequests.VIERW_INSERT_TARGET_DATA);

        try {
            if (request.getParameter("insert") != null) {
                final List<TransactionTarget> retrievedTransformedData =
                    this.transactionDataService.retrieveTransformedData();

                this.transactionDataService.insertTransactionDetails(retrievedTransformedData);
                mov.addObject("message", "Transaction data are inserted to the target table.");
            }
        } catch (Exception e) {
            request.setAttribute("error", Boolean.TRUE);
            request.setAttribute("message", "Error occured while inserting target data.");
            e.printStackTrace();
        }

        return mov;
    }

    public ModelAndView loadData() throws Exception {
        ModelAndView mov = new ModelAndView("/admin/transaction/transaction_summary");

        this.transactionDataService.loadTransactionDataFromTable();

        return mov;
    }
}



